<%@ page language="java" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="m" uri="/cutm_tags"%>
<html>
<head>
<title>串联单编排</title>
<link rel="stylesheet" href="<s:url value="/common/css/datepicker.css"/>" type="text/css" />
<script type="text/javascript" src="<s:url value="/common/scripts/ui.datepicker.js"/>"></script>
<script type="text/javascript" src="<s:url value="/common/scripts/ui.datepicker-fr.js"/>"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('#sel').hide();
	$('#btn').hide();
	$('#passAudit').hide();
	$('#remark').hide();
	$('#auditNow').hide();
	$('#editInfo').hide();
	$('#searchDaoYu').hide();
	$('#commit').click(function() {
		commit();
	});
	$('#editInfo').click(function() {
		editInfo();
	});
	$('#searchDaoYu').click(function() {
		searchDaoYu();
	});
	$("#serchForm").vaildAjax(function(data){
		
	});
	$('#lone').click(function() {
		left2right();
	});
	$('#lall').click(function() {
		all2right();
	});
	$('#rone').click(function() {
		right2left();
	});
	$('#rall').click(function() {
		all2left();
	});
	$('#up').click(function() {
		upListItem();
	});
	$('#down').click(function() {
		downListItem();
	});
	$('#left').dblclick(function() {
		left2right();
	});
	$('#right').dblclick(function() {
		right2left();
	});
	$('#doAudit').val(${doAudit});
	if(${doAudit}==true) {
		$('#commit').val('审核');
		$('#audit').addClass('required');
	}
});
function left2right() {
	var text = $('#left').sltTxt();
	var val = $('#left').select();
	if(text=='') {
		alert('请选择稿件');
		return;
	}
	var timeL=$('#timeL').text();
	var timeR=$('#timeR').text();
	var timeA=text.split('-');
	var time=timeA[0];
	$('#timeL').text(parseInt(timeL)-parseInt(time));
	$('#timeR').text(parseInt(timeR)+parseInt(time));
	$('#right').addOpt(text, val);
	$('#right').val(val);
	$('#left').delOpt(val,false);
}
function all2right() {
	var leftall = $('#left').html();
	var rightall = $('#right').html();
	var timeL=$('#timeL').text();
	var timeR=$('#timeR').text();
	$('#timeR').text(parseInt(timeL)+parseInt(timeR));
	$('#timeL').text(0);
	//$('#right').copy('right' + leftall);
	$('#right').html(leftall + rightall);
	$('#left').empSelect();
}
function all2left() {
	var leftall = $('#right').html();
	var rightall = $('#left').html();
	var timeL=$('#timeL').text();
	var timeR=$('#timeR').text();
	$('#timeL').text(parseInt(timeL)+parseInt(timeR));
	$('#timeR').text(0);
	$('#left').html(leftall + rightall);
	$('#right').empSelect();
}
function right2left() {
	var text = $('#right').sltTxt();
	var val = $('#right').select();
	if(text=='') {
		alert('请选择稿件');
		return;
	}
	var timeL=$('#timeL').text();
	var timeR=$('#timeR').text();
	var timeA=text.split('-');
	var time=timeA[0];
	$('#timeL').text(parseInt(timeL)+parseInt(time));
	$('#timeR').text(parseInt(timeR)-parseInt(time));
	$('#left').addOpt(text, val);
	$('#left').val(val);
	$('#right').delOpt(val,false);
}
//向上移动
function upListItem() {
	var listObj=document.getElementById('right');
	var selIndex=listObj.selectedIndex;
	if(selIndex<0)
	{
		alert(" 请选择需要上移的一行！");
		return;
	}
	if(selIndex==0)
	{
		alert(" 已经在第一位了！");
		return;
	}
	var selValue=listObj.options[selIndex].value;
	var selText=listObj.options[selIndex].text;
	listObj.options[selIndex].value=listObj.options[selIndex-1].value;
	listObj.options[selIndex].text=listObj.options[selIndex-1].text;
	listObj.options[selIndex-1].value=selValue;
	listObj.options[selIndex-1].text=selText;
	listObj.selectedIndex=selIndex-1;
}
//向下移动、
function downListItem()
{
	var listObj=document.getElementById('right');
	var selIndex=listObj.selectedIndex;
	if(selIndex<0)
	{
		alert("请选择需要上移的一行！");
		return;
	}
	if(selIndex==listObj.options.length-1)
	{
		alert(" 已经在最后一位了！");
		return;
	}
	var selValue=listObj.options[selIndex].value;
	var selText=listObj.options[selIndex].text;
	listObj.options[selIndex].value=listObj.options[selIndex+1].value;
	listObj.options[selIndex].text=listObj.options[selIndex+1].text;
	listObj.options[selIndex+1].value=selValue;
	listObj.options[selIndex+1].text=selText;
	listObj.selectedIndex=selIndex+1;
}
//组装左边选择框
function buildLeft(data) {
	$("#left").empSelect();
	var timeL=0;
	for (var i = 0;i < data.length; i++) {
		timeL=timeL+data[i].timeLong;
		$('select[name="left"]').addOpt(
				data[i].timeLong+ '--' + data[i].times  + '--' + data[i].userName + '--'
						+ data[i].title, data[i].id);
	}
	$('#timeL').text(timeL);
}

//组装右边框
function buildRight(data) {
	$("#right").empSelect();
	var timeR=0;
	for (var i = 0;i < data.length; i++) {
		timeR=timeR+data[i].timeLong;
		$('select[name="right"]').addOpt(
				data[i].timeLong+ '--' + data[i].times + '--' + data[i].userName + '--'
						+ data[i].title, data[i].id);
	}
	$('#timeR').text(timeR);
}

function getInfor() {
	var beginDate = $('input[name=beginDate]').val();
	var endDate = $('input[name=endDate]').val();
	var title = $('input[name=manuscript.title]').val();
	var mscolumn = $('select[name=manuscript.column] option:selected').val();
	var pdate = $('input[name=programming.date]').val();
	var pcolumn = $('select[name=programming.column] option:selected').val();
	var used = $('input[name=used]:checked').val();
	var url='programming/SearchProgram!search' + '?used='+used+'&beginDate=' + beginDate+'&endDate='+endDate+'&manuscript.column='+mscolumn+'&manuscript.title='+title+'&programming.date='+pdate+'&programming.column='+pcolumn;
	$.urlAjax(url,function(data) {
				$('#programId').val(data[2].id);
				if((data[1]==null || data[1].length==0) && $('#doAudit').val()=='true') {
					//alert('此栏目没有编排的信息，不能审核');
					BaseJs.alert('此栏目没有编排的信息，不能审核','err');
					return;
				}
				if(data[2].auditUser!=null) {
					$('#auditUser').text(data[2].auditUser.name);
					$('#auditDate').text(data[2].auditDate);
					$('#passAudit').show();
				}
				if((data[2].auditRemark!=null && data[2].auditRemark!='') || $('#doAudit').val()=='true') {
					$('#auditRemark').val(data[2].auditRemark);
					$('#remark').show();
				}
				if($('#doAudit').val()=='true') {
					var i = data[2].audit == true ? 0 : 1;
					//$('#audit').val(true);
					//alert('data[2].audit:'+data[2].audit);
					//$('input[name="programming.audit"]').attr("checked",data[2].audit);
					//$('input[name="programming.audit"][@value="true"]').attr("checked",true);
					$('input[name="programming.audit"]').get(i).checked = true; 					
					$('#auditNow').show();
					if(data[2].audit==true) {
						//BaseJs.setReadonly();
						//$('#commit').hide();
					}
				}
				$('#getInfo').hide();
				$('#sel').show();
				$('#btn').show();
				if(data[2].auditUser!=null && data[3]==false&& data[2].audit == true) {
					$('#commit').hide();
				}
				buildLeft(data[0]);
				buildRight(data[1]);
			});
}
//提交
function commit(){
 	var idlist = $('#right').sltValAry();
	if(idlist!=''){
		if(BaseJs.confirm('确定要提交吗？')){
			var pdate = $('input[name=programming.date]').val();
			var pcolumn = $('select[name=programming.column] option:selected').val();
			var audit = $('input[name=programming.audit]:checked').val();
			if($('#doAudit').val()=='true' && audit==undefined) {
				BaseJs.alert('请填写审核结果','err');
				return;
			}
			var progId=$('#programId').val();
			var auditRemark=encodeURIComponent($('.auditRemark').val());
			if($('#doAudit').val()=='true' && auditRemark=='') {
				BaseJs.alert('请填写审核意见','err');
				return;
			}
			var doAudit=$('#doAudit').val();
			var url='programming/EditProgram!editProgram' + '?programming.id='+progId+'&programming.date='+pdate+'&programming.column='+pcolumn+'&programming.auditRemark='+auditRemark+'&programming.audit='+audit+'&idlist='+idlist+'&doAudit='+doAudit;
			$.urlAjax(url,function(data) {
				$('#programId').val(data);
				BaseJs.alert('保存成功！');
				BaseJs.setReadonly();
				$('#commit').hide();
				$('#editInfo').hide();
				$('#searchDaoYu').hide();
			});
		}
	}else{
		 BaseJs.alert('请选择编排的稿件','err');
	}
	
}
//编排栏目详细信息
function editInfo(){
	var programId = $('#programId').val();
	if(programId!=''){
		var url=BaseJs.action('programming/SearchProgram!searchProgm2GaoJian')+'?programming.id='+programId;
		window.open(url);
	}else{
		  BaseJs.alert('请选择编排的节目','err');
	}
}
//查看导语
function searchDaoYu() {
	var programId = $('#programId').val();
	if(programId!=''){
		var url=BaseJs.action('programming/SearchProgram!searchDaoYu')+'?programming.id='+programId;
		window.open(url);
	}else{
		  BaseJs.alert('请选择编排的节目','err');
	}
}
</script>
</head>
<body>
<h2 class="title"><span>串联单编排</span></h2>
<s:form id="serchForm" action="programming/EditProgram!editProgram" onsubmit="false">
	<h3>请输入查询条件</h3>
	<s:hidden name="programId" id="programId" />
	<s:hidden name="idlist" id="idlist" />
	<s:hidden name="doAudit" id="doAudit" />
	<table id="sTable" class="form">
		<tr>
			<th width="20%">撰稿日期</th>
			<td width="40%"><m:date name="beginDate" maxDate="0" />-<m:date name="endDate" maxDate="0" /></td>
			<th width="20%">撰稿栏目</th>
			<td width="20%"><m:select enumType="program.type" name="manuscript.column" headKey="--请选择--" /></td>
		</tr>
		<tr>
			<th>稿件标题</th>
			<td><s:textfield name="manuscript.title" maxlength="255" /></td>
			<th>稿件是否录用</th>
			<td><s:radio list="#{0:'全部',1:'已用',2:'未用'}" name="used"></s:radio> </td>
		</tr>
		<tr>
			<th><span class="hint">*</span>编排日期</th>
			<td><m:date name="programming.date" cssClass="rule[required]" /></td>
			<th><span class="hint">*</span>编排栏目</th>
			<td><m:select enumType="program.type" name="programming.column" cssClass="rule[required]" /></td>
		</tr>
		<tr id="getInfo">
			<td colspan="4" align="center"><input value="查询" type="button" onclick="getInfor();"></td>
		</tr>
		<tr id="sel">
			<td colspan="4">
			<table class="form">
				<tr>
					<td width="45%"><label>时长(秒)--使用次数(次)--记者--标题</label><br>
					<label>【稿件】</label><br>
					<select style="height: 300px; width: 500px;" id="left" name="left" size="75%" /></select></td>
					<td width="10%" valign="middle"><br>
					<div id="btnGrp"><input style="width: 40px;" id="lone" type="button" value="->"><br>
					<br>
					<input style="width: 40px;" type="button" id="lall" value="-->>"><input style="width: 40px;" type="button"
						id="up" value="上移"><br>
					<br>
					<input style="width: 40px;" type="button" id="rone" value="<-"><input style="width: 40px;" type="button"
						id="down" value="下移"><br>
					<br>
					<input style="width: 40px;" type="button" id="rall" value="<<--"><br>
					<br>
					</div>
					</td>
					<td width="45%"><label>时长(秒)--使用次数(次)--记者--标题</label><br>
					<label>【编排新闻】</label><br>
					<select id="right" class="required" style="height: 300px; width: 500px;" name="right" size="75%" /></select></td>
				</tr>
				<tr>
					<td>时长总计:<s:label id="timeL" /> 秒</td>
					<td></td>
					<td>时长总计:<s:label id="timeR" /> 秒</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr id="passAudit">
			<th>审核人</th>
			<td><s:label id="auditUser" /></td>
			<th>审核时间</th>
			<td><s:label id="auditDate" /></td>
		</tr>
		<tr id="auditNow">
			<th>审核是否通过</th>
			<td><s:radio list="#{true:'是',false:'否'}" name="programming.audit" id="audit"></s:radio></td>
			<th></th>
			<td></td>
		</tr>
		<tr id="remark">
			<th><span class="hint">*</span>审核意见</th>
			<td colspan="3"><s:textarea name="programming.auditRemark" id="auditRemark" cssClass="auditRemark"></s:textarea></td>
		</tr>
	</table>
	<div class="btnGroup" id="btn"><input value="保存" type="button" id="commit"><input value="编排栏目详细信息"
		type="button" id="editInfo"> <input value="查看导语" type="button" id="searchDaoYu"></div>
</s:form>
</body>
</html>